﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraBars;
using AxMOVIEPLAYERLib;
using GOM_PLAYER.control_subtitle;

namespace GOM_PLAYER
{
    public partial class control_panel : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private main_window mainWindow;
        private string urlFile;
        public control_panel(main_window mainWindow)
        {
            this.mainWindow = mainWindow;
            InitializeComponent();
            this.size_sub_trackBarControl.Value = SubtitleHelper.DEFAUL_SIZE_FONT;
            this.x_sub_trackBarControl.Value = (int)(SubtitleHelper.DEFAUL_X*100);
            this.y_sub_trackBarControl.Value = (int)(SubtitleHelper.DEFAUL_Y*100);

            this.pitch_trackBar.Minimum = 0;
            this.pitch_trackBar.Maximum = 24;
            this.pitch_trackBar.Value = 12;
        }

        public control_panel()
        {
            InitializeComponent();
        }

        private void view_photo_simpleButton_Click(object sender, EventArgs e)
        {
            if(urlFile != null)
            {
                view_photo vp = new view_photo();
                vp.Show();
                vp.pictureEdit1.LoadAsync(urlFile);

            }
        }

        private void control_panel_Load(object sender, EventArgs e)
        {
            path_folder_textBox.Text = mainWindow.urlSaveCap;
            if (mainWindow.setMaintainSpeed)
                maintain_play_speed_checkButton.Checked = true;
            if (mainWindow.set_AB_Repeat)
            {
                time_repeat_A_textBox.Text = mainWindow.convert_to_time((int)mainWindow.startPos);
                time_repeat_B_textBox.Text = mainWindow.convert_to_time((int)mainWindow.endPos);
            }

            if (mainWindow.time_backward == 60)
            {
                backward_dropDownButton.ToolTip = "Backward 60s";
                backward30s_barCheckItem.Checked = true;
            }
            else
            {
                if (mainWindow.time_backward == 30)
                {
                    backward_dropDownButton.ToolTip = "Backward 30s";
                    backward30s_barCheckItem.Checked = true;
                }
                else
                {
                    backward_dropDownButton.ToolTip = "Backward 10s";
                    backward10s_barCheckItem.Checked = true;
                }
            }

            if (mainWindow.time_forward == 60)
            {
                forward_dropDownButton.ToolTip = "Forward 60s";
                forward30s_barCheckItem.Checked = true;
            }
            else
            {
                if (mainWindow.time_forward == 30)
                {
                    forward_dropDownButton.ToolTip = "Forward 30s";
                    forward30s_barCheckItem.Checked = true;
                }
                else
                {
                    forward_dropDownButton.ToolTip = "Forward 10s";
                    forward10s_barCheckItem.Checked = true;
                }
            }
        }

        private void stat_A_B_repeat_Click(object sender, EventArgs e)
        {
            mainWindow.startPos = mainWindow.axMoviePlayer.GetPos();
            time_repeat_A_textBox.Text = mainWindow.current_position_textbox.Text;
            time_repeat_B_textBox.Text = mainWindow.convert_to_time((int)mainWindow.endPos);
            mainWindow.set_AB_Repeat = true;
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "AB Repeat: " + mainWindow.convert_to_time((int) mainWindow.startPos) + " - " +
                       mainWindow.convert_to_time((int) mainWindow.endPos);
            tr.Start(s);
        }

        private void end_A_B_repeat_Click(object sender, EventArgs e)
        {
            mainWindow.endPos = mainWindow.axMoviePlayer.GetPos();
            time_repeat_B_textBox.Text = mainWindow.current_position_textbox.Text;
            if (mainWindow.startPos == 0)
                time_repeat_A_textBox.Text = "00:00:00";
            mainWindow.set_AB_Repeat = true;
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "AB Repeat: " + mainWindow.convert_to_time((int)mainWindow.startPos) + " - " +
                       mainWindow.convert_to_time((int)mainWindow.endPos);
            tr.Start(s);
        }

        private void Undo_AB_repeat_button_Click(object sender, EventArgs e)
        {
            mainWindow.startPos = 0;
            mainWindow.endPos = mainWindow.axMoviePlayer.Duration;
            time_repeat_A_textBox.Text = "00:00:00";
            time_repeat_B_textBox.Text = "00:00:00";
            mainWindow.set_AB_Repeat = false;
            Thread tr = new Thread(mainWindow.showMessage);
            tr.Start("Undo AB Repeat");
        }

        private void backward10s_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            backward_dropDownButton.ToolTip = "Backward 10s";
            backward10s_barCheckItem.Checked = true;
            backward30s_barCheckItem.Checked = false;
            backward60s_barCheckItem.Checked = false;
        }

        private void backward30s_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {          
            mainWindow.time_backward = 30;
            backward_dropDownButton.ToolTip = "Backward 30s";
            backward10s_barCheckItem.Checked = false;
            backward30s_barCheckItem.Checked = true;
            backward60s_barCheckItem.Checked = false;
        }

        private void backward60s_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            mainWindow.time_backward = 60;
            backward_dropDownButton.ToolTip = "Backward 60s";
            backward10s_barCheckItem.Checked = false;
            backward30s_barCheckItem.Checked = false;
            backward60s_barCheckItem.Checked = true;
        }

        private void backward_dropDownButton_Click(object sender, EventArgs e)
        {
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Backward: " + mainWindow.time_backward.ToString() + "s";
            tr.Start(s);
            double pos = mainWindow.axMoviePlayer.GetPos() - mainWindow.time_backward;
            if (pos > 0)
            {
                mainWindow.axMoviePlayer.SetPos(pos);
            }
            else
            {
                mainWindow.axMoviePlayer.SetPos(0);
            }
            
        }

        private void forward10s_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            mainWindow.time_forward = 10;
            forward_dropDownButton.ToolTip = "Forward 10s";
            forward10s_barCheckItem.Checked = true;
            forward30s_barCheckItem.Checked = false;
            forward60s_barCheckItem.Checked = false;
        }

        private void forward30s_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            mainWindow.time_forward = 30;
            forward_dropDownButton.ToolTip = "Forward 30s";
            forward10s_barCheckItem.Checked = false;
            forward30s_barCheckItem.Checked = true;
            forward60s_barCheckItem.Checked = false;
        }

        private void forward60s_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            mainWindow.time_forward = 60;
            forward_dropDownButton.ToolTip = "Forward 60s";
            forward10s_barCheckItem.Checked = false;
            forward30s_barCheckItem.Checked = false;
            forward60s_barCheckItem.Checked = true;
        }

        private void forward_dropDownButton_Click(object sender, EventArgs e)
        {
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Forward: " + mainWindow.time_forward.ToString() + "s";
            tr.Start(s);
            double pos = mainWindow.axMoviePlayer.GetPos() + mainWindow.time_forward;
            if (pos < mainWindow.axMoviePlayer.Duration)
            {
                mainWindow.axMoviePlayer.SetPos(pos);
            }
            else
            {
                mainWindow.axMoviePlayer.SetPos(mainWindow.axMoviePlayer.Duration - 1);
            }
        }

       
        private void speedup_simpleButton_Click(object sender, EventArgs e)
        {
            if (mainWindow.axMoviePlayer.PlaybackRate < 2)
            {
                mainWindow.axMoviePlayer.PlaybackRate = mainWindow.axMoviePlayer.PlaybackRate + 0.2;
                mainWindow.axMoviePlayer.SetPosByMilliSec(mainWindow.axMoviePlayer.GetPosByMilliSec() - 0.0001);
                mainWindow.axMoviePlayer.RegisterMsg();
            }
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Speed: " + mainWindow.axMoviePlayer.PlaybackRate.ToString() + "x";
            tr.Start(s);
        }

        private void speeddown_simpleButton_Click(object sender, EventArgs e)
        {
            if (mainWindow.axMoviePlayer.PlaybackRate > 0.2)
            {
                mainWindow.axMoviePlayer.PlaybackRate = mainWindow.axMoviePlayer.PlaybackRate - 0.2;
                mainWindow.axMoviePlayer.SetPosByMilliSec(mainWindow.axMoviePlayer.GetPosByMilliSec() + 0.0001);
                mainWindow.axMoviePlayer.RegisterMsg();
            }
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Speed: " + mainWindow.axMoviePlayer.PlaybackRate.ToString() + "x";
            tr.Start(s);
        }

        private void speednormal_simpleButton_Click(object sender, EventArgs e)
        {
            mainWindow.axMoviePlayer.PlaybackRate = 1;
            mainWindow.axMoviePlayer.SetPosByMilliSec(mainWindow.axMoviePlayer.GetPosByMilliSec() - 0.0001);
            mainWindow.axMoviePlayer.RegisterMsg();
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Speed: " + mainWindow.axMoviePlayer.PlaybackRate.ToString() + "x";
            tr.Start(s);
        }

        private void maintain_play_speed_checkButton_Click(object sender, EventArgs e)
        {
            if (!mainWindow.setMaintainSpeed)
            {
                mainWindow.setMaintainSpeed = true;
                Thread tr = new Thread(mainWindow.showMessage);
                tr.Start("Set Maintain Play Speed");
            }
            else
            {
                mainWindow.setMaintainSpeed = false;
                Thread tr = new Thread(mainWindow.showMessage);
                tr.Start("Undo Maintain Play Speed");
            }
        }
        private void size_sub_trackBarControl_Scroll(object sender, EventArgs e)
        {
            try
            {
                this.mainWindow.playSub.FontSize = (short)this.size_sub_trackBarControl.Value;
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Subtitle size: " + (short)this.size_sub_trackBarControl.Value;
                tr.Start(s);
            }
            catch (Exception){}
            
        }

        private void y_sub_trackBarControl_Scroll(object sender, EventArgs e)
        {
            try
            {
                this.mainWindow.playSub.PosY = this.y_sub_trackBarControl.Value / 100F;
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Subtitle:(" + this.x_sub_trackBarControl.Value+"; "+this.y_sub_trackBarControl.Value+")";
                tr.Start(s);
            }
            catch(Exception) { }
        }

        private void x_sub_trackBarControl_Scroll(object sender, EventArgs e)
        {
            try
            {
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Subtitle:(" + this.x_sub_trackBarControl.Value + "; " + this.y_sub_trackBarControl.Value + ")";
                this.mainWindow.playSub.PosX = this.x_sub_trackBarControl.Value / 100F;
                tr.Start(s);
            }
            catch (Exception)
            { }
        }

        private void reset_size_sub_simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                this.size_sub_trackBarControl.Value = SubtitleHelper.DEFAUL_SIZE_FONT;
                this.mainWindow.playSub.FontSize = 30;
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Reset subtitle size";
                tr.Start(s);
            }
            catch (Exception) { }
        }

        private void reset_y_simpleButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.y_sub_trackBarControl.Value = (int)(SubtitleHelper.DEFAUL_Y * 100);
                this.mainWindow.playSub.PosY = SubtitleHelper.DEFAUL_Y;
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Reset subtitle position";
                tr.Start(s);
            }
            catch (Exception) { }
        }

        private void reset_x_simpleButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.x_sub_trackBarControl.Value = (int)(SubtitleHelper.DEFAUL_X * 100);
                this.mainWindow.playSub.PosX = SubtitleHelper.DEFAUL_X;
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Reset subtitle position";
                tr.Start(s);
            }
            catch (Exception) { }
        }

        private void faster0_5_simpleButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.mainWindow.playSub.previousSub();
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Previous subtitle";
                tr.Start(s);
            }
            catch (Exception)
            { }
        }

        private void slower_0_5_simpleButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.mainWindow.playSub.nextSub();
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Next subtitle";
                tr.Start(s);
            }
            catch (Exception)
            { }
        }

       
        private void left_chanel_radioButton_CheckedChanged(object sender, EventArgs e)
        {
      
            if(left_chanel_radioButton.Checked)
            {
                this.mainWindow.axMoviePlayer.SetMPEG1AudioChannel(1);
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Change left chanel";
                tr.Start(s);
            }
        }

        private void right_chanel_radioButton_CheckedChanged(object sender, EventArgs e)
        {
           
            if (right_chanel_radioButton.Checked)
            {
                this.mainWindow.axMoviePlayer.SetMPEG1AudioChannel(0);
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Change right chanel";
                tr.Start(s);
            }
        }

        private void both_chanel_radioButton_CheckedChanged(object sender, EventArgs e)
        {

            if (both_chanel_radioButton.Checked)
            {
                this.mainWindow.axMoviePlayer.SetMPEG1AudioChannel(2);
                Thread tr = new Thread(mainWindow.showMessage);
                string s = "Change both chanel";
                tr.Start(s);
            }
        }

        private void screen_capture_simpleButton_Click(object sender, EventArgs e)
        {

            urlFile = mainWindow.urlSaveCap + "\\" + mainWindow.axMoviePlayer.GetPosByMilliSec().ToString() + ".jpg";
            mainWindow.axMoviePlayer.SnapShot2ImageFile(urlFile, 0);
            Capture_Picbox.Load(urlFile);
            Thread tr = new Thread(mainWindow.showMessage);
            tr.Start("Screen Capture");
        }

        private void change_folder_simpleButton_Click(object sender, EventArgs e)
        {
    
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    mainWindow.urlSaveCap = fbd.SelectedPath;
                    path_folder_textBox.Text = mainWindow.urlSaveCap;
                }
            }
        }

        private void open_folder_simpleButton_Click(object sender, EventArgs e)
        {

            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "Image Files(*.bmp;*.jpg;*.gif;*.png;*.ico)| (*.bmp;*.jpg;*.gif;*.png;*.ico)";
                ofd.InitialDirectory = mainWindow.urlSaveCap;
                ofd.RestoreDirectory = false;
                if (ofd.ShowDialog(this) == DialogResult.OK)
                {
                    view_photo vp = new view_photo();
                    vp.Show();
                    vp.pictureEdit1.Image = new Bitmap(ofd.OpenFile());
                }
            }
        }

        private void Default_pitch_simpleButton_Click(object sender, EventArgs e)
        {
            this.pitch_trackBar.Value = 12;
            this.mainWindow.axMoviePlayer.ChangePitch((short)(this.pitch_trackBar.Value - 12));
            this.mainWindow.pitch = (short)(this.pitch_trackBar.Value - 12);
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Default pitch:" + (this.pitch_trackBar.Value-12);
            tr.Start(s);
        }

        private void pitch_trackBar_Scroll(object sender, EventArgs e)
        {
            this.mainWindow.axMoviePlayer.ChangePitch((short)(this.pitch_trackBar.Value - 12));
            this.mainWindow.pitch = (short)(this.pitch_trackBar.Value - 12);
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Change pitch:" + (this.pitch_trackBar.Value-12);
            tr.Start(s);
        }

        private void close_button_Click(object sender, EventArgs e)
        {
            this.Hide();
            mainWindow.temp_control_panel_button++;
        }

        private void resest_brightness_button_Click(object sender, EventArgs e)
        {
            brightness_trackBarControl.Value = 5;
            Thread tr = new Thread(mainWindow.showMessage);
            tr.Start("Brightness: 50");
        }

        private void reset_contrast_button_Click(object sender, EventArgs e)
        {
            contrast_trackBarControl.Value = 5;
            Thread tr = new Thread(mainWindow.showMessage);
            tr.Start("Contrast: 50");
        }

        private void reset_saturation_button_Click(object sender, EventArgs e)
        {
            saturation_trackBarControl.Value = 5;
            Thread tr = new Thread(mainWindow.showMessage);
            tr.Start("Saturation: 50");
        }

        private void reset_hue_button_Click(object sender, EventArgs e)
        {
            hue_trackBarControl.Value = 5;
            Thread tr = new Thread(mainWindow.showMessage);
            tr.Start("Hue: 50");
        }

        private void brightness_trackBarControl_EditValueChanged(object sender, EventArgs e)
        {
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Brightness: " + (brightness_trackBarControl.Value*10).ToString();
            tr.Start(s);
        }

        private void contrast_trackBarControl_EditValueChanged(object sender, EventArgs e)
        {
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Contrast: " + (contrast_trackBarControl.Value * 10).ToString();
            tr.Start(s);
        }

        private void saturation_trackBarControl_EditValueChanged(object sender, EventArgs e)
        {
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Saturation: " + (saturation_trackBarControl.Value * 10).ToString();
            tr.Start(s);
        }

        private void hue_trackBarControl_EditValueChanged(object sender, EventArgs e)
        {
            Thread tr = new Thread(mainWindow.showMessage);
            string s = "Hue: " + (hue_trackBarControl.Value * 10).ToString();
            tr.Start(s);
        }

        private void use_voice_adjustment_checkButton_CheckedChanged(object sender, EventArgs e)
        {
            Thread tr = new Thread(mainWindow.showMessage);
            tr.Start("Use Voice Adjustment");
        }
        
    }
}